package com.changgou.pay.service.impl;

import com.changgou.pay.service.WxPayService;
import com.github.wxpay.sdk.WXPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    private WXPay wxPay;

    @Value("${wxpay.notify_url}")
    private String notify_url;
    /**
     * 支付下单
     * @param orderId
     * @param payMoney
     * @return
     *      {
     *         "nonce_str": "MQXs5ZhQxDLN4vOA",
     *         "code_url": "weixin://wxpay/bizpayurl?pr=NBfaVhf",
     *         "appid": "wx8397f8696b538317",
     *         "sign": "1572B5017115DD9C73E040FFBE7C69C2D463AE91254FFF687792246A0A0BEB5E",
     *         "trade_type": "NATIVE",
     *         "return_msg": "OK",
     *         "result_code": "SUCCESS",
     *         "mch_id": "1473426802",
     *         "return_code": "SUCCESS",
     *         "prepay_id": "wx27104346454190258840d8f51924753200"
     *     }
     */
    @Override
    public Map<String, Object> nativePay(String orderId, Integer payMoney) {
        try {
            //请求参数封装
            Map reqData = new HashMap();
            reqData.put("body","畅购商城");//商品描述
            reqData.put("out_trade_no",orderId); //商户订单号
            //一分钱支付
            BigDecimal money = new BigDecimal("0.01");
            BigDecimal total_fee = money.multiply(new BigDecimal(100));//0.01 * 100 = 1
            //如果小数点后两位,还有数字,需要进行四舍五入
            total_fee = total_fee.setScale(0,BigDecimal.ROUND_UP);//四舍五入,保留小数点后两位.
            reqData.put("total_fee",String.valueOf(total_fee)); //标价金额,以分为单位
            reqData.put("spbill_create_ip","127.0.0.1");//终端ip
//            reqData.put("notify_url","http://www.itcast.cn");//回调地址,必须外网能够访问的地址.
            reqData.put("notify_url","http://limingxuan.cross.echosite.cn/wxpay/notify");//回调地址,必须外网能够访问的地址.
//            reqData.put("notify_url",notify_url);//回调地址,必须外网能够访问的地址.
            reqData.put("trade_type","NATIVE");//交易类型

            Map mapResult = wxPay.unifiedOrder(reqData);//调用统一下单api
            System.out.println("统一下单 : "+mapResult);
            return mapResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过Map返回结果,查看查询订单是否成功,是否支付成功的结果
     *      return_code : SUCCESS 返回的结果
     *      result_code : SUCCESS 业务结果
     *      trade_state : SUCCESS 支付成功
     * @param orderId
     * @return
     */
    @Override
    public Map queryOrder(String orderId) {
        try {
            //封装查询订单参数
            Map reqData = new HashMap();
            reqData.put("out_trade_no",orderId);
            Map mapResult = wxPay.orderQuery(reqData);
            return mapResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map closeOrder(String orderId) {
        try {
            Map reqData = new HashMap();
            reqData.put("out_trade_no",orderId);
            Map map = wxPay.closeOrder(reqData);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
